﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;

namespace CS3230HealthTrackerSystemGUI
{
    public partial class OrderTestForm : Form
    {
        private String testID;
        private String testDate;
        private String testTime;
        private String patientID;
        private String doctorID;
        private String nurseID;
        private String resultDate;
        private String testResult;
        private String diagnosis;

        private Validation validate = new Validation();
        private Decryption key = new Decryption();
        private FieldValueHolder fields = new FieldValueHolder();

        private String MyConString;


        public OrderTestForm()
        {
            InitializeComponent();
        }

        public OrderTestForm(FieldValueHolder field)
        {
            fields = field;

            orderTestDateTextBox.Text = fields.getCurrentDate();
            orderTimeTextBox.Text = fields.getCurrentTime();
            orderPatientIDTextBox.Text = fields.getCurrentPatientID();
            orderTestDoctorIDTextBox.Text = fields.getCurrentDoctorID();
            orderTestNurseIDTextBox.Text = fields.getCurrentNurseID();
        }

        private void orderTestRecordButton_Click(object sender, EventArgs e)
        {
            try
            {
                //Gathers data from each of the text boxes and verify the data is valid. 
                testID = orderTestIDTextBox.Text.Trim();
                testDate = orderTestDateTextBox.Text;
                testTime = orderTimeTextBox.Text;
                patientID = orderPatientIDTextBox.Text;
                doctorID = orderTestDoctorIDTextBox.Text;
                nurseID = orderTestNurseIDTextBox.Text;
                resultDate = orderTestResultDateTextBox.Text;
                testResult = orderTestResultTextBox.Text;
                diagnosis = orderDiagnosisTextBox.Text;

                //Validates all the fields.
                validate.validateID(patientID, "Patient");
                validate.validateID(doctorID, "Doctor");
                validate.validateID(nurseID, "Nurse");
                validate.validateDate(testDate);
                validate.validateTime(testTime);
                //validate.validateTestResults(testResult);

                //Converts all the information into the appropriate data types.
                int currentPatient = Int32.Parse(patientID);
                int currentDoctor = Int32.Parse(doctorID);
                int currentNurse = Int32.Parse(nurseID);

                //Assigns Connection string.    
                MyConString = key.returnConnectionString("xxjrmy", "xxjrmy", "xx3kz4z");
                OdbcConnection dataAccess = new OdbcConnection(MyConString);
                //Creates statement to insert all the information into the database. 
                string myInsertQuery = "INSERT INTO TEST (testID, dateTestPerformed, timeTestPerformed, dateResultRecieved, testResult, patientTested, doctorOrdering, nurseRecording) VALUES " +
                    "('" + testID + "', '" + testDate +"', '" + testTime + "', '" + resultDate +"', '" + testResult +"', '" + patientID + "', '" + doctorID +"', '" +nurseID +"')";
                //Sets up the connection, and Inserts the data.
                OdbcCommand insertCommand = new OdbcCommand(myInsertQuery);
                insertCommand.Connection = dataAccess;
                dataAccess.Open();
                insertCommand.ExecuteNonQuery();

                //Closes the insert command.
                insertCommand.Connection.Close();

                MessageBox.Show("Current test information recorded.");
                orderTestIDTextBox.Clear();
                orderTimeTextBox.Clear();
            }

            //Throws an exception if something is incorrectly formatted.
            catch (Exception formatException)
            {
                MessageBox.Show(formatException.Message);
            }
        }

        private void completeTests_Click(object sender, EventArgs e)
        {

            try
            {
                //Gathers data from each of the text boxes and verify the data is valid. 
             
                testDate = orderTestDateTextBox.Text;
                patientID = orderPatientIDTextBox.Text;

                //Validates all the fields.
                validate.validateID(patientID, "Patient");             
                validate.validateDate(testDate);

                //Converts all the information into the appropriate data types.
                int currentPatient = Int32.Parse(patientID);
             

                //Assigns Connection string.
                MyConString = key.returnConnectionString("xxjrmy", "xxjrmy", "xx3kz4z");
                OdbcConnection dataAccess = new OdbcConnection(MyConString);

                //Holds the values to be stored into the dataGridview to be displayed.
                List<StringHolder> list = new List<StringHolder>();

                OdbcConnection MyConnection = new OdbcConnection(MyConString);

                MyConnection.Open();

                string selectTestInfo = "SELECT * FROM `TEST` WHERE dateTestPerformed='" + testDate + "' AND patientTested = '" + patientID + "'";
                OdbcCommand MyCommand = new OdbcCommand(selectTestInfo, MyConnection);
                OdbcDataReader MyDataReader;
                MyDataReader = MyCommand.ExecuteReader();


                while (MyDataReader.Read())
                {
                    //Creates a string to hold the values from the query.
                    String builder = " ";
                    //Checks the number of columns that are going to be readin, and for each column.
                    int values = MyDataReader.VisibleFieldCount;
                    for (int currentColumn = 0; currentColumn < values; currentColumn++)
                    {
                        //If the value in the current column is null.
                        if (MyDataReader.IsDBNull(currentColumn) == true)
                        {
                            builder += " " + MyDataReader.GetName(currentColumn) + " : " + " NULL ";
                        }
                        else
                        {
                            //Checks for the type of data in the column, and handles it appropriately.
                            //For each it checks the type, gets the column name, and then the value.
                            String type = MyDataReader.GetDataTypeName(currentColumn);

                            if (type == "varchar")
                            {
                                builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetString(currentColumn).ToString() + " ";
                            }
                            if (type == "integer")
                            {
                                builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetInt32(currentColumn).ToString() + " ";
                            }
                            if (type == "decimal")
                            {
                                builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetDecimal(currentColumn).ToString() + " ";
                            }
                            if (type == "date")
                            {
                                builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetDate(currentColumn).ToShortDateString() + " ";
                            }
                        }
                    }
                    //Stores the string in the string wrapper.
                    StringHolder currentString = new StringHolder(builder);
                    //Adds the string to be added into the data grid view.
                    list.Add(currentString);
                }
                //Displays the data in the datagridview.
                dataDisplay.DataSource = list;
                dataDisplay.Show();

                MessageBox.Show("Tests ordered.");
                //Close all resources
                MyDataReader.Close();
                MyConnection.Close();

            }

              //Throws an exception if something is incorrectly formatted.
            catch (Exception formatException)
            {
                MessageBox.Show(formatException.Message);
            }
        }


    }
}
